package com.lyxs916.testlibrary.loadingtest;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.lyxs916.widget.factory.CustomDialogFactory;
import com.lyxs916.widget.factory.CustomLoadingFactory;
import com.lyxs916.widget.loading.bar.LoadingBar;
import com.lyxs916.widget.loading.bar.OnLoadingBarListener;
import com.lyxs916.widget.loading.dialog.LoadingDialog;
import com.lyxs916.testlibrary.R;

/**
 * LoadingBar的例子
 * author  dengyuhan
 * created 2017/4/16 11:38
 */
public class LoadingBarActivity extends Activity {
    private View mParent;
    private View mParent2;
    private Handler mHandler = new Handler();

    public static void startUi(Context context) {
        Intent intent = new Intent(context, LoadingBarActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_activity_loadingbar);

        mParent = findViewById(R.id.loading_container);
        mParent2 = findViewById(R.id.llk);

    }

    //显示Loading
    public void clickLoading(View v) {
        //默认的bar
        LoadingBar.make(mParent).show();

        LoadingBar.make(mParent2,R.layout.test_activity_main).show();
    }


//    //显示Loading
//    public void clickLoading(View v) {
//        //默认的bar
//        LoadingBar.make(mParent).show();
//
////        LoadingBar.make(mParent,R.layout.rr_activity_main).show();
//    }

    //自定义Loading
    public void clickCustomLoading(View v) {
        LoadingBar.make(mParent, new CustomLoadingFactory())
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(LoadingBarActivity.this, "点击了", Toast.LENGTH_SHORT).show();
                    }
                }).setOnLoadingBarListener(new OnLoadingBarListener() {
            @Override
            public void onCancel(View parent) {
                Toast.makeText(LoadingBarActivity.this, "Loading取消了", Toast.LENGTH_SHORT).show();
            }
        }).show();

//
//        LoadingBar.make(mParent,new CustomLoadingFactory()).setOnLoadingBarListener(new OnLoadingBarListener() {
//            @Override
//            public void onCancel(View parent) {
//
//            }
//        }).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });
    }

    //隐藏Loading
    public void clickCancelLoading(View v) {
        LoadingBar.cancel(mParent);
    }

    //列表显示Loading
    public void clickListLoading(View v) {
//        startActivity(new Intent(this, ListLoadingActivity.class));
    }

    //显示LoadingDialog
    public void clickDefault(View v) {
        LoadingDialog.make(this).show();

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                LoadingDialog.cancel();
            }
        }, 2000);
    }

    private EditText edMessage;
    private CheckBox cbCancelable;

    //自定义LoadingDialog
    public void clickCustom(View v) {

        edMessage = (EditText) this.findViewById(R.id.ed_message);
        cbCancelable = (CheckBox) this.findViewById(R.id.cb_cancelable);

        Dialog dialog = LoadingDialog.make(this, new CustomDialogFactory())
                .setMessage(edMessage.getText())//提示消息
                .setCancelable(cbCancelable.isChecked())
                .create();

        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                Toast.makeText(LoadingBarActivity.this, "Dialog取消了", Toast.LENGTH_SHORT).show();
            }
        });
        dialog.show();

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                LoadingDialog.cancel();
            }
        }, 2000);
    }

}
